﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using PowerCreator.LMS.Web.Project;
using System.Collections.Generic;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Project.Entity;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Common.Script;

public partial class Theme_3_MyProject : PowerCreator.LMS.Web.Base.BasePage
{
    ProjectStudent opProjectStudent = new ProjectStudent();
    ProjectUser opProjectUser = new ProjectUser();
    NewProjectPlan opProjectPlan = new NewProjectPlan();
    Project opProject = new Project();
    ProjectResource opProjectResource = new ProjectResource();
    Course opCourse = new Course();
    ManagerPaper opPaper = new ManagerPaper();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "UserLogin.aspx";
    PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        this.Title = config.GetLanagerValueByKey("theme_Study_MyProjects", PowerCreator.LMS.Language.LanagetItemType.Page);

        if (!IsPostBack)
        {
            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }

    }


    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append(string.Format(" SELECT TO_Class.Name, TO_Class.Plans_Id, TO_Class.Batch_Id, TO_Class.[Rule], TO_UserOfClass.UserID, TO_UserOfClass.ClassID,TO_Class.ClassCourseListID, TO_CourseList.CourseList_Id, TO_CourseList.CourseListName FROM TO_UserOfClass ,TO_Class,TO_CourseList Where TO_UserOfClass.ClassID = TO_Class.Class_Id AND TO_Class.ClassCourseListID = TO_CourseList.CourseList_Id and TO_CourseList.Used=1 and TO_UserOfClass.UserID={0}", login.UserID));

        return sbSearch.ToString();
    }

    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = PowerCreator.LMS.Web.Config.WebConfig.PageSize;
        AspNetPager1.RecordCount = obj.Count(search);
    }
    private void BinderRPT(string search, int pageIndex)
    {
        //string search = " SELECT TO_Class.Name, TO_Class.Plans_Id, TO_Class.Batch_Id, TO_Class.[Rule], TO_UserOfClass.UserID, TO_UserOfClass.ClassID,TO_Class.ClassCourseListID, TO_CourseList.CourseList_Id, TO_CourseList.CourseListName FROM TO_UserOfClass ,TO_Class,TO_CourseList Where TO_UserOfClass.ClassID = TO_Class.Class_Id AND TO_Class.ClassCourseListID = TO_CourseList.CourseList_Id and TO_CourseList.Used=1 and TO_UserOfClass.UserID=" + login.UserID;
        DataTable dt = new DataTable();
        dt = obj.Query(search);

        rpt_TrainProject.DataSource = dt;
        rpt_TrainProject.DataBind();

        //bool bldog = PowerCreator.LMS.SCR.Dog.CheckDog();

        //for (int i = 0; i < rpt_TrainProject.Items.Count; i++)
        //{
        //    if (!bldog)
        //    {
        //        if (i != 0)
        //        {
        //            rpt_TrainProject.Items[i].Visible = false;
        //            AspNetPager1.Visible = false;
        //        }
        //    }
        //    Label lbl_ProjectID = (Label)rpt_TrainProject.Items[i].FindControl("lbl_ProjectID");
        //    Label lbl_ClassID = (Label)rpt_TrainProject.Items[i].FindControl("lbl_ClassID");
        //    Label lbl_ProjectResourceList = (Label)rpt_TrainProject.Items[i].FindControl("lbl_ProjectResourceList");
        //    Repeater rpt_ProjectResourceList = (Repeater)rpt_TrainProject.Items[i].FindControl("rpt_ProjectResourceList");
        //    //BinderRPTProjectResource(rpt_ProjectResourceList, lbl_ProjectID.Text,lbl_ClassID.Text);

        //    Label lbl_StudentState = (Label)rpt_TrainProject.Items[i].FindControl("lbl_StudentState");
        //    Label lbl_StudentStateName = (Label)rpt_TrainProject.Items[i].FindControl("lbl_StudentStateName");
        //    if (lbl_StudentState.Text.ToLower() == "false")
        //    {
        //        LinkButton lnkbtn_study = (LinkButton)rpt_TrainProject.Items[i].FindControl("lnkbtn_study");
        //        LinkButton LinkButton1 = (LinkButton)rpt_TrainProject.Items[i].FindControl("LinkButton1");
        //        lnkbtn_study.Enabled = false;
        //        LinkButton1.Enabled = false;
        //    }
        //    else if (lbl_StudentStateName.Text == "<span style='color:#666'>未开始</span>")
        //    {

        //        PowerCreator.LMS.Project.Entity.VOTTrainClass voProjectClass = opProjectUser.LoadProjectClass(int.Parse(lbl_ClassID.Text));
        //        PowerCreator.LMS.Project.Entity.VOTTrainProject voProject = opProject.LoadTrainProject(voProjectClass.FKTrainclaTrainproID);
        //        if (voProject.TrainproSatate == 1)
        //        {
        //            info.ProjectID = voProjectClass.FKTrainclaTrainproID;
        //            info.UserID = login.UserID;
        //            if (voProjectClass.TrainclaID == int.MinValue)
        //                info.ClassID = 0;
        //            else
        //                info.ClassID = voProjectClass.TrainclaID;
        //            opProjectPlan.AddProjectPlan(info);
        //        }
        //    }
            
        //    lbl_ProjectResourceList.Text = BinderRPTProjectResource(lbl_ProjectID.Text, lbl_ClassID.Text, lbl_StudentState.Text);
           
        //}


    }



    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rpt_TrainProject_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "lstudy")
        {
            //Label lbl_StudentStateName = (Label)e.Item.FindControl("lbl_StudentStateName");         //资源当前状态

            //NewProjectPlanInfo info = new NewProjectPlanInfo();
            //PowerCreator.LMS.Project.Entity.VOTTrainClass voProjectClass = opProjectUser.LoadProjectClass(int.Parse(e.CommandArgument.ToString()));
            //PowerCreator.LMS.Project.Entity.VOTTrainProject voProject = opProject.LoadTrainProject(voProjectClass.FKTrainclaTrainproID);
            //Label lbl_rpt_msg = (Label)e.Item.FindControl("lbl_rpt_msg");
            //if (voProject.TrainproSatate != 1)
            //{
            //    if (voProject.TrainproSatate == 0)
            //        lbl_rpt_msg.Text = "方案己被冻结!";
            //    else if (voProject.TrainproSatate == 2)
            //        lbl_rpt_msg.Text = "方案正在审核中!";
            //    return;
            //}

            //info.ProjectID = voProjectClass.FKTrainclaTrainproID;
            //if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
            //{
            //    lbl_rpt_msg.Text = "未登录或登录超时!请重新登录!";
            //    return;
            //}
            //info.UserID = login.UserID;
            //if (voProjectClass.TrainclaID == int.MinValue)
            //    info.ClassID = 0;
            //else
            //    info.ClassID = voProjectClass.TrainclaID;
            ////opProjectPlan.AddProjectPlan(info);

            //string url = "StudyProject.aspx?ProjectID=" + voProjectClass.FKTrainclaTrainproID + "&ClassID=" + e.CommandArgument.ToString();
            //PowerCreator.LMS.Common.Script.Script.RedirectURL(url);

            Label lbl_CourseListID = (Label)e.Item.FindControl("lbl_CourseListID");  //课程表ID
            string url = "StudyProject.aspx?ClassID=" + e.CommandArgument.ToString() + "&CourseListID=" + lbl_CourseListID.Text;
            PowerCreator.LMS.Common.Script.Script.RedirectURL(url);

        }
    }




    //private string BinderRPTProjectResource(string pid, string cid, string StudentState)
    //{
    //    string html = "";
    //    string starturl = "";
    //    string endurl = "";
    //    string target = string.Empty;
    //    string search = string.Format("FKProresTrainproID = {0}", pid);
    //    string orderList = "ProresResIsQueue desc,ProresIndex desc";
    //    DataTable dt = opProjectResource.QueryProjectResource(search, orderList, opProjectResource.QueryProjectResourceCount(search), 1);
    //    List<ProjectResourceInfo> list = FillList(dt, pid, cid);
    //    NewProjectPlanInfo info = new NewProjectPlanInfo();
    //    info.ProjectID = int.Parse(pid);
    //    info.ClassID = int.Parse(cid);
    //    info.UserID = GetLoginEntity.UserID;

    //    html = "<div class=\"featurebox\">";
    //    html += "<div class=\"featureleft\">";
    //    html += "<div class=\"feature\">";
    //    html += " <div class=\"galleryholder\">";
    //    html += " <div class=\"gallerycontent\">";
    //    html += " <ul id=\"ulGallery\" style=\"width:1800px; margin-left:10px;\">";
    //    int j = 0;
    //    for (int i = 0; i < list.Count; i++)
    //    {
    //        VOTProjectResource voProjectResource = opProjectResource.LoadProjectResource(list[i].ProjectResourceID);
    //        //如果当前资源设置为有序学习
    //        if (StudentState.ToLower() != "false")
    //        {
    //            if (list[i].ProjectResourceProresClass == 1)
    //            {
    //                target = string.Empty;
    //                starturl = "<a href=\"OpenCourse.aspx?CourseID=" + voProjectResource.FKProresCourseID.ToString() + "&ProjectID=" + pid + "&ProjectClassID=" + cid + "&ProjectResourceID=" + voProjectResource.ProresID.ToString() + "&StudyCourseState=" + PowerCreator.LMS.Common.Utils.Base64.Encode(list[i].ProjectResourcePassState.ToString()) + "\">";
    //                endurl = "</a>";
    //                if (list[i].ProresResIsQueue.ToString() == "1")
    //                {
    //                    if (i != 0)
    //                    {
    //                        j = i - 1;
    //                        if (list[j].ProjectResourcePassState.ToString() != "1")
    //                        {
    //                            starturl = ""; 
    //                            endurl="";
    //                        }
    //                    }
    //                }
    //            }

    //            else if (list[i].ProjectResourceProresClass == 2)
    //            {

    //                if (list[i].ProresResIsQueue.ToString() == "1")
    //                {
    //                    if (i != 0)
    //                    {
    //                        j = i - 1;
    //                        if (list[j].ProjectResourcePassState.ToString() != "1")
    //                        {
    //                            starturl = "";
    //                            endurl = "";
    //                        }
    //                        else
    //                        {
    //                            //target = "target=_blank";
    //                            endurl = "</a>";
    //                            string bukao = "0"; //是否补考0：不是，1：是	
    //                            int exampapercount = opProjectPlan.QueryExamPaperCount(voProjectResource, info, 0);
    //                            if (exampapercount != 0)
    //                                bukao = "1";
    //                            if (exampapercount == 0)
    //                                starturl = "<a href=\"#\" onclick=\"OpenPaper('"+Server.UrlEncode("../../web/UserObject/OpenPaperList.aspx?ProjectID=" + pid + "&ProjectClassID=" + cid + "&CourseID=0&IsBuKao=" + bukao + "&ProjectResourceID=" + voProjectResource.ProresID + "&CourseResourceID=0&ResourceID=" + voProjectResource.FKProresPaperID )+"')\">";
    //                            else
    //                            {
    //                                if (voProjectResource.ProresPaperMakeupNum <= exampapercount)
    //                                {
    //                                    if (list[i].ProjectResourcePassState == 4)
    //                                    {
    //                                        starturl = "<a  href=\"#\" onclick=\"OpenPaper('"+Server.UrlEncode("../../web/UserObject/OpenPaperList.aspx?ProjectID=" + pid + "&ProjectClassID=" + cid + "&CourseID=0&IsBuKao=" + bukao + "&ProjectResourceID=" + voProjectResource.ProresID + "&CourseResourceID=0&ResourceID=" + voProjectResource.FKProresPaperID )+"')\" >";
    //                                    }
    //                                    else
    //                                    {
    //                                        starturl = "";
    //                                        endurl = "";
    //                                    }
    //                                }

    //                                else
    //                                    starturl = "<a  href=\"#\" onclick=\"OpenPaper('"+Server.UrlEncode("../../web/UserObject/OpenPaperList.aspx?ProjectID=" + pid + "&ProjectClassID=" + cid + "&CourseID=" + voProjectResource.FKProresCourseID + "&IsBuKao=" + bukao + "&ProjectResourceID=" + voProjectResource.ProresID + "&CourseResourceID=0&ResourceID=" + voProjectResource.FKProresPaperID )+"')\">";
    //                            }

    //                        }
    //                    }
    //                    else
    //                    {

    //                        //target = "target=_blank";
    //                        endurl = "</a>";
    //                        string bukao = "0"; //是否补考0：不是，1：是	
    //                        int exampapercount = opProjectPlan.QueryExamPaperCount(voProjectResource, info, 0);
    //                        if (exampapercount != 0)
    //                            bukao = "1";
    //                        if (exampapercount == 0)
    //                            starturl = "<a  href=\"#\" onclick=\"OpenPaper('"+Server.UrlEncode("../../web/UserObject/OpenPaperList.aspx?ProjectID=" + pid + "&ProjectClassID=" + cid + "&CourseID=0&IsBuKao=" + bukao + "&ProjectResourceID=" + voProjectResource.ProresID + "&CourseResourceID=0&ResourceID=" + voProjectResource.FKProresPaperID )+"')\">";
    //                        else
    //                        {
    //                            if (voProjectResource.ProresPaperMakeupNum <= exampapercount)
    //                            {
    //                                if (list[i].ProjectResourcePassState == 4)
    //                                {
    //                                    starturl = "<a  href=\"#\" onclick=\"OpenPaper('"+Server.UrlEncode("../../web/UserObject/OpenPaperList.aspx?ProjectID=" + pid + "&ProjectClassID=" + cid + "&CourseID=0&IsBuKao=" + bukao + "&ProjectResourceID=" + voProjectResource.ProresID + "&CourseResourceID=0&ResourceID=" + voProjectResource.FKProresPaperID)+"')\">";
    //                                }
    //                                else
    //                                {
    //                                    starturl = "";
    //                                    endurl = "";
    //                                }
    //                            }

    //                            else
    //                                starturl = "<a  href=\"#\" onclick=\"OpenPaper('"+Server.UrlEncode("../../web/UserObject/OpenPaperList.aspx?ProjectID=" + pid + "&ProjectClassID=" + cid + "&CourseID=" + voProjectResource.FKProresCourseID + "&IsBuKao=" + bukao + "&ProjectResourceID=" + voProjectResource.ProresID + "&CourseResourceID=0&ResourceID=" + voProjectResource.FKProresPaperID )+"')\">";
    //                        }

    //                    }

    //                }
    //                else
    //                {
    //                    //target = "target=_blank";
    //                    endurl = "</a>";
    //                    string bukao = "0"; //是否补考0：不是，1：是	
    //                    int exampapercount = opProjectPlan.QueryExamPaperCount(voProjectResource, info, 0);
    //                    if (exampapercount != 0)
    //                        bukao = "1";
    //                    if (exampapercount == 0)
    //                        starturl = "<a  href=\"#\" onclick=\"OpenPaper('"+Server.UrlEncode("../../web/UserObject/OpenPaperList.aspx?ProjectID=" + pid + "&ProjectClassID=" + cid + "&CourseID=0&IsBuKao=" + bukao + "&ProjectResourceID=" + voProjectResource.ProresID + "&CourseResourceID=0&ResourceID=" + voProjectResource.FKProresPaperID)+"')\">";
    //                    else
    //                    {
    //                        if (voProjectResource.ProresPaperMakeupNum <= exampapercount)
    //                        {
    //                            if (list[i].ProjectResourcePassState == 4)
    //                            {
    //                                starturl = "<a  href=\"#\" onclick=\"OpenPaper('"+Server.UrlEncode("../../web/UserObject/OpenPaperList.aspx?ProjectID=" + pid + "&ProjectClassID=" + cid + "&CourseID=0&IsBuKao=" + bukao + "&ProjectResourceID=" + voProjectResource.ProresID + "&CourseResourceID=0&ResourceID=" + voProjectResource.FKProresPaperID )+"')\">";
    //                            }
    //                            else
    //                            {
    //                                starturl = "";
    //                                endurl = "";
    //                            }
    //                        }

    //                        else
    //                            starturl = "<a  href=\"#\" onclick=\"OpenPaper('" + Server.UrlEncode("../../web/UserObject/OpenPaperList.aspx?ProjectID=" + pid + "&ProjectClassID=" + cid + "&CourseID=" + voProjectResource.FKProresCourseID + "&IsBuKao=" + bukao + "&ProjectResourceID=" + voProjectResource.ProresID + "&CourseResourceID=0&ResourceID=" + voProjectResource.FKProresPaperID) + "')\">";
    //                    }

    //                }

    //            }

    //        }

    //        html += " <li style=\"background-color: transparent;\">";
    //        html += "      <p> " + starturl + "<img alt=\"" + list[i].ResourceName + "\" src=\"" + list[i].ResourceImageUrl + "\" width=\"40\" height=\"30\">" + endurl + "</p>";
    //        html += "      <p class=\"caption\">" + starturl + "<em>" + PowerCreator.LMS.Common.Utils.StringUtils.GetSubstringLue(list[i].ResourceName, 4) + "</em>" + endurl + "</p>";
    //        html += " </li>";

    //    }


    //    html += " </ul>";
    //    html += "</div>";
    //    html += " </div> ";
    //    html += " </div></div>";
    //    html += "</div>";
    //    html += "  </div>";
    //    return html;
    //}
    //private List<ProjectResourceInfo> FillList(DataTable dt,string pid,string cid)
    //{
    //    List<ProjectResourceInfo> list = new List<ProjectResourceInfo>();

    //    for (int i = 0; i < dt.Rows.Count; i++)
    //    {
    //        ProjectResourceInfo item = new ProjectResourceInfo();
    //        item.ProjectID = int.Parse(pid);
    //        item.ProjectResourceProresClass = int.Parse(dt.Rows[i]["ProresClass"].ToString());
    //        item.ProjectResourceID = int.Parse(dt.Rows[i]["ProresID"].ToString());
    //        item.ProjectResourceProresClassName = opProjectResource.GetProjectResourceProresClass(item.ProjectResourceProresClass);
    //        item.ProresResIsQueue = dt.Rows[i]["ProresResIsQueue"].ToString(); 

    //        //string searchplan = string.Format("FKSpcoelogTrainproID = {0} and FKSpcoelogTrainclaID = {1} and FKSpcoelogUserID = {2} and FKSpcoelogProresID = {3} ", item.ProjectID, ViewState["ProjectClassID"], login.UserID, item.ProjectResourceID);
    //        string searchplan = string.Format("FKSpcoelogTrainproID = {0} and FKSpcoelogProresID = {1} and FKSpcoelogTrainclaID={2} and FKSpcoelogUserID={3}", item.ProjectID, item.ProjectResourceID, cid,login.UserID);
    //        item.ProjectResourcePassState = 4;
    //        DataTable dtPlan = opProjectPlan.QueryProjectResource(searchplan, 1, 1);
    //        if (dtPlan.Rows.Count == 1)
    //        {
    //            item.ProjectResourcePassState = int.Parse(dtPlan.Rows[0]["SpcoelogState"].ToString());
    //        }
    //        item.ProjectResourcePassStateName = opProjectPlan.GetProjectPlanByState(item.ProjectResourcePassState, ProjectPlan.ProjectResourcePlan);

    //        if (item.ProjectResourceProresClass == Convert.ToInt16(ProjectResourceProresClass.Course))//课程
    //        {
    //            item.ProresCourseResIsQueue = opProjectResource.GetProjectResourceProresCourseResIsQueue(int.Parse(dt.Rows[i]["ProresCourseResIsQueue"].ToString()));
    //            item.ProresPaperMakeupNum = "-";
    //            item.StartTime = "-";
    //            item.EndTime = "-";
    //            item.ResourceID = int.Parse(dt.Rows[i]["FKProresCourseID"].ToString());
    //            item.ResourceCount = 0;
    //            item.ProresCoursePassCondition = dt.Rows[i]["ProresCoursePassCondition"].ToString();
    //            item.ProresPaperPassScore = "-";
    //            VOTOCourse vo = opCourse.LoadVOTCourse(item.ResourceID);
    //            if (vo != null)
    //            {
    //                item.ResourceName = vo.CourseName;
    //                item.ResourceImageUrl = vo.CourseImgUrl;
    //            }
    //        }
    //        else if (item.ProjectResourceProresClass == Convert.ToInt16(ProjectResourceProresClass.Paper))  //考试
    //        {
    //            item.ProresCourseResIsQueue = "-";
    //            item.ProresPaperMakeupNum = dt.Rows[i]["ProresPaperMakeupNum"].ToString();
    //            item.ResourceCount = 1;
    //            item.ResourceID = int.Parse(dt.Rows[i]["FKProresPaperID"].ToString());
    //            item.StartTime = dt.Rows[i]["ProresPaperStartTime"].ToString();
    //            item.EndTime = dt.Rows[i]["ProresPaperEndTime"].ToString();
    //            item.ProresPaperPassScore = dt.Rows[i]["ProresPaperPassScore"].ToString();
    //            item.ProresCoursePassCondition = "-";
    //            item.ResourceImageUrl = "images/Exam.jpg";
    //            //item.ResourceAuthor = "——";
    //            VOPaper vo = opPaper.LoadPaper(item.ResourceID);
    //            if (vo != null)
    //            {
    //                item.ResourceName = vo.PaperName;
    //                //item.ResourceDescription = vo.PaperName;
    //            }
    //        }

    //        list.Add(item);
    //    }
    //    return list;
    //}



 


    private string GetPicName(string fileName, int Resourceclass)
    {
        string rt = "images/Exam.jpg";
        if (Resourceclass == 1)
        {
            rt = "000.jpg";
        }
        else if (Resourceclass == 2)
        {
            string[] fileNames = fileName.Split('.');
            if (fileNames.Length == 2)
                rt = fileNames[0].ToString() + ".jpg";
        }
        return rt;
    }
    private string GetPath(string Path, int Resourceclass)
    {
        string rt = Path;
        if (Resourceclass != 1 && Resourceclass != 2)
        {
            rt = "";
        }
        return rt;
    }


}
